/*
 * $Id: CommonLogger.java,v 1.2 2009/12/21 15:59:55 navdeep_mahajan Exp $
 * 
 * Copyright (C) 2008 General Electric Company. All Rights Reserved.
 * 
 * This software is the confidential and proprietary information of the General
 * Electric Company (GE). You shall not disclose this software and shall use it
 * only in accordance with the terms of the license agreement you entered into
 * with GE.
 * 
 * GE MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF THE
 * SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, OR
 * NON-INFRINGEMENT. GE SHALL NOT BE LIABLE FOR ANY DAMAGES SUFFERED BY LICENSEE
 * AS A RESULT OF USING, MODIFYING, OR DISTRIBUTING THIS SOFTWARE OR ITS
 * DERIVATIVES.
 */
package com.ge.healthcare.autosc.common.dbLogger;

import java.net.UnknownHostException;

import com.ge.healthcare.autosc.common.ASCLogger;
import com.ge.healthcare.autosc.common.ASCUtil;
import com.ge.healthcare.autosc.common.database.dao.IStepTypeDAO;
import com.ge.healthcare.autosc.common.database.pojo.CfgStepType;
import com.ge.healthcare.autosc.common.exceptions.MBeanAccessException;

public abstract class CommonLogger {

	protected static Long stepTypeId = null;
	protected static String stepTypeName = null;
	protected static ILogSender logSender = null;
	protected static String managedServerName = null;
	protected static IStepTypeDAO stepTypeDAO = null;
	protected static final String LOG_DELIM = "\n\n";
	
	/**
	 * Returns managed server name, for tests it returns hardware hostname
	 * 
	 * @return
	 * @throws UnknownHostException
	 */
	protected static String getServerName() throws UnknownHostException {
		String serverName = null;
		String methodName = "getServerName";
		try {
			serverName = ASCUtil.getManagedServerName();
		} catch (MBeanAccessException mBeanAccessFailure) {
			ASCLogger
					.error(
							StepLogger.class,
							methodName,
							"If i am seeing this log it means i am running a test, i should never see the log in production");
			serverName = ASCUtil.getHardwareHostName();
		}
		return serverName;
	}
	
	/**
	 * This method retrieves the step type id if defined If not defined it
	 * creates a record using the step type name
	 * 
	 * @param stepTypeName
	 *            - Step type Name
	 * @throws UnknownHostException
	 *             - Unable to get the hostname for the current machine
	 */
	protected static void updateStepTypeId(String stepTypeName)
			throws UnknownHostException {
		String methodName = "updateStepTypeId";
		ASCLogger.debug(ContactLogger.class, methodName, "Entering " + methodName);
		managedServerName = getServerName();
		stepTypeId = stepTypeDAO.getStepTypeID(stepTypeName, managedServerName);
		ASCLogger
				.info(ContactLogger.class, methodName, "stepTypeId:" + stepTypeId);
		if (ASCUtil.isNullOrEmptyObject(stepTypeId)) {
			CfgStepType stepType = new CfgStepType();
			stepType.setStepType(stepTypeName);
			stepType.setServerName(managedServerName);
			stepTypeDAO.save(stepType);
			ASCLogger.info(ContactLogger.class, methodName, "Saved stepType:"
					+ stepType);
			stepTypeId = stepType.getStepTypeId();
			ASCLogger.info(ContactLogger.class, methodName,
					"stepTypeId after save:" + stepTypeId);
		}
		ASCLogger.debug(ContactLogger.class, methodName, "Leaving " + methodName);
	}
	
	/**
	 * @param logSender
	 *            the logSender to set
	 */
	public void setLogSender(ILogSender logSender) {
		CommonLogger.logSender = logSender;
	}

	/**
	 * @return the stepTypeName
	 */
	public static String getStepTypeName() {
		return stepTypeName;
	}

	/**
	 * @return the managedServerName
	 */
	public static String getManagedServerName() {
		return managedServerName;
	}
}
